package com.loong.order.handler;

import com.loong.common.enumerate.OrderStatus;
import com.loong.common.enumerate.PaymentStatus;
import com.loong.common.enumerate.PaymentType;
import com.loong.common.enumerate.ResultCode;
import com.loong.common.exception.GlobalException;
import com.loong.order.common.forehead.result.OrderBaseResult;
import com.loong.order.model.OrderBase;
import com.loong.order.model.PaymentOrder;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 订单支付服务
 * @Author: hzp
 * @Date: 2021-1-11 10:18
 */
@Component
public class PaymentHandler {
    @Resource
    private OrderBaseHandler orderBaseHandler;

    @Resource
    private PaymentOrderHandler paymentOrderHandler;
    /**
     * 货到付款 下单是否完成
     * @param userId 用户id
     * @param orderId 订单id
     */
    //todo 常量暂时写在这里
    public Boolean cashOnDelivery(Long userId,Long orderId){
        OrderBaseResult orderBaseResult = orderBaseHandler.findById(orderId);
        BigDecimal payment = orderBaseResult.getPayment();
        if(payment.compareTo(new BigDecimal(500))>0){
            throw new GlobalException(ResultCode.CASH_ON_DELIVERY_EXCESS);
        }
        List<OrderBase> OrderBaseList = orderBaseHandler.findByCondition(userId, PaymentType.CASH_ON_DELIVERY, OrderStatus.WAITING_PAYMENT_AND_IN_DELIVERY);
        if(CollectionUtils.isEmpty(OrderBaseList)){
            //更新订单状态为 待支付配送中 只有货到付款有的状态
            orderBaseHandler.update(buildOrderBase(orderId));
            return true;
        }
        if(OrderBaseList.size()>5){
            throw new GlobalException(ResultCode.CASH_ON_DELIVERY_COUNT);
        }
        BigDecimal reduce = OrderBaseList.stream().map(OrderBase::getPayment).reduce(BigDecimal.ZERO, BigDecimal::add);
        if(reduce.compareTo(new BigDecimal(1000))>0){
            throw new GlobalException(ResultCode.CASH_ON_DELIVERY_PAYMENT_NUM);
        }
        //更新订单状态为 待支付配送中 只有货到付款有的状态
        orderBaseHandler.update(buildOrderBase(orderId));
        PaymentOrder paymentOrder = paymentOrderHandler.findByOrderId(orderId);
        paymentOrder.setPaymentType(PaymentType.CASH_ON_DELIVERY.getCode());
        paymentOrder.setPaymentStatus(PaymentStatus.WAITING_NOT_RECEIVED.getCode());
        paymentOrderHandler.update(paymentOrder);
        return true;

    }

    /**
     * 构建 待支付 配送中的 数据模型 只有货到付款有的
     * @param orderId 订单id
     * @return OrderBase
     */
    public OrderBase buildOrderBase(Long orderId){
        OrderBase orderBase = new OrderBase();
        orderBase.setId(orderId);
        orderBase.setStatus(OrderStatus.WAITING_PAYMENT_AND_IN_DELIVERY.getCode());
        orderBase.setPaymentType(PaymentType.CASH_ON_DELIVERY.getCode());
        return orderBase;
    }

    public Boolean checkCashOnDelivery(Long userId, Long orderId) {
        BigDecimal payment = orderBaseHandler.findById(orderId).getPayment();
        if(payment.compareTo(new BigDecimal(500))>0){
            throw new GlobalException(ResultCode.CASH_ON_DELIVERY_EXCESS);
        }
        List<OrderBase> OrderBaseLists = orderBaseHandler.findByCondition(userId, PaymentType.CASH_ON_DELIVERY, OrderStatus.WAITING_PAYMENT_AND_IN_DELIVERY);
        if(CollectionUtils.isEmpty(OrderBaseLists)){
            return true;
        }
        if(OrderBaseLists.size()>5){
            throw new GlobalException(ResultCode.CASH_ON_DELIVERY_COUNT);
        }
        BigDecimal reduce = OrderBaseLists.stream().map(OrderBase::getPayment).reduce(BigDecimal.ZERO, BigDecimal::add);
        if(reduce.compareTo(new BigDecimal(1000))>0){
            throw new GlobalException(ResultCode.CASH_ON_DELIVERY_PAYMENT_NUM);
        }
        return true;
    }
}
